package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.suyuan.entity.PigIn;
import com.suyuan.service.PigInService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.web.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生猪出入厂控制器
 */
@Controller
@RequestMapping(value ="/admin/PigIn")
public class PigInController  {

    @Resource(name = "pigInService")
    private PigInService pigInService;

    private Map<String,Object> msg ;

    /**
     * 多条件查询生猪信息
     * @param beginTime
     * @param endTime
     * @param purGid
     * @return
     */
    @RequestMapping("findByCondition")
    public String listPigIns(Date beginTime, Date endTime, Integer purGid, Page page, ModelMap modelMap){
        PageInfo pageInfo = pigInService.inquiry(beginTime,endTime,purGid,page);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("purGid",purGid);
        return "pigIn_list";
    }

    /**
     * 跳转到编辑页面
     * @param
     * @return
     */
    @RequestMapping("toedit")
    public String toEdit(Integer gid, ModelMap modelMap){
        PigIn pigIn = pigInService.findByPID(gid);
        modelMap.addAttribute("pigIn",pigIn);
        return "pigIn_edit";
    }


    /**
     * 更新信息
     * @param pigIn
     * @return
     */
    @RequestMapping("edit")
    @ResponseBody
    public Object editInfo(PigIn pigIn){
        int code = 0;
        try{
            pigInService.updatePigIn(pigIn);
            code = 1;
        }catch (Exception e){
            code = 0;
        }
        return code;
    }



    /**
     * 跳转到添加生猪入场信息页面
     * @return
     */
    @RequestMapping("toAddPigInPage")
    public String toAddPigInPage(){
        return "pigIn_add";
    }


    /**
     * 添加生猪入场信息
     * @return
     */
    @RequestMapping("addPigIn")
    @ResponseBody
    public Object addPigIn(PigIn pigIn){
        int code = 0;
        try{
           pigInService.addPigIn(pigIn);
           code = 1;
        }catch (Exception e){
            code = 0;
        }
        return code;
    }

    /**
     * 删除生猪入场信息
     * @param
     * @return
     */
    @RequestMapping("deletes")
    @ResponseBody
    public Object deletePigOutInfo(String ids){
        Integer code = pigInService.deletePigInInfo(ids);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }else{
            return JsonMessage.error("删除失败");
        }
    }

    /**
     * 单个删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Integer deletePigOut(Integer id){
        int code = pigInService.deletePig(id);
        return code;
    }

    /**
     * 检验生猪编号是否存在
     */
    @RequestMapping("checkPigIn")
    @ResponseBody
    public Object checkPig(Integer id){
        Boolean flag = pigInService.checkPig(id);
        if(flag){
            return JsonMessage.success("生猪信息有效");
        }else{
            return JsonMessage.error("生猪信息无效");
        }
    }


}
